package com.xry.order.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xry.common.bean.Order;
import com.xry.common.bean.Order;
import com.xry.common.service.OrderService;
import com.xry.common.service.OrderService;
import com.xry.order.mapper.OrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author 86152
 * @description 针对表【order】的数据库操作Service实现
 * @createDate 2025-09-04 19:58:40
 */
@Service
@DubboService
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {
    @Override
    public Order getOrderById(Integer id) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getId, id).eq(Order::getDeleted, 0);
        return super.getOne(queryWrapper);
    }


    @Override
    public boolean updateOrderById(Order order) {
        return super.updateById(order);
    }

    @Override
    public boolean deleteOrderById(Integer id) {
        Order order = super.getById(id);
        if (order != null){
            order.setDeleted(1);
            return super.updateById( order);
        }
        return false;
    }

    @Override
    public boolean addOrder(Order order) {
        order.setOrderId("order-"+System.currentTimeMillis());
        return super.save(order);
    }

    @Override
    public List<Order> getAll() {
        return super.list();
    }

    @Override
    public IPage<Order> getPage(Order order, IPage<Order> page) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();

        // 只查询未删除的记录
        queryWrapper.eq(Order::getDeleted, 0);

        // 添加查询条件
        if (order != null) {
            // 用户ID精确查询
            if (order.getUserId() != null && order.getUserId() > 0) {
                queryWrapper.eq(Order::getUserId, order.getUserId());
            }

            // 比赛ID精确查询
            if (order.getMatchId() != null && order.getMatchId() > 0) {
                queryWrapper.eq(Order::getMatchId, order.getMatchId());
            }

            // 座位ID精确查询
            if (order.getSeatId() != null && order.getSeatId() > 0) {
                queryWrapper.eq(Order::getSeatId, order.getSeatId());
            }

            // 订单状态精确查询
            if (order.getStatus() != null) {
                queryWrapper.eq(Order::getStatus, order.getStatus());
            }

            // 金额范围查询（大于等于最小金额）
            if (order.getAmount() != null) {
                queryWrapper.ge(Order::getAmount, order.getAmount());
            }

            // 创建时间范围查询（大于等于创建时间）
            if (order.getCreatetime() != null) {
                queryWrapper.ge(Order::getCreatetime, order.getCreatetime());
            }
        }

        // 按创建时间倒序排列，最新订单在前
        queryWrapper.orderByDesc(Order::getCreatetime);

        return super.page(page, queryWrapper);
    }
}


